package com.dycx.user.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.user.converter.VehicleBrandConverter;
import com.dycx.user.dto.VehicleBrandDTO;
import com.dycx.user.entity.VehicleBrandDO;
import com.dycx.user.service.VehicleBrandService;
import com.dycx.user.vo.VehicleBrandVO;
import com.dycx.user.mapper.VehicleBrandMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车辆表(VehicleBrand)表服务实现类
 *
 * @author wuzongbing
 * @since 2025-01-19 18:06:06
 */

@Service
@AllArgsConstructor
public class VehicleBrandImpl extends ServiceImpl<VehicleBrandMapper, VehicleBrandDO> implements VehicleBrandService {

    private VehicleBrandMapper vehicleBrandMapper;

    /**
     * 分页查询
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public Page<VehicleBrandVO> getPage(VehicleBrandDTO dto) {
        LambdaQueryWrapper<VehicleBrandDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(dto.getBrandName()), VehicleBrandDO::getBrandName, dto.getBrandName());
        wrapper.orderByDesc(VehicleBrandDO::getId);

        Page<VehicleBrandDO> entityPage = page(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);

        Page<VehicleBrandVO> voPage = VehicleBrandConverter.entityToVoPage(entityPage);
        return voPage;
    }

    /**
     * 查询列表
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public List<VehicleBrandVO> getList(VehicleBrandDTO dto) {
        LambdaQueryWrapper<VehicleBrandDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(dto.getStatus()), VehicleBrandDO::getStatus, dto.getStatus());
        wrapper.orderByDesc(VehicleBrandDO::getId);

        List<VehicleBrandDO> entityList = list(wrapper);

        return VehicleBrandConverter.entityToVoList(entityList);
    }

    /**
     * 查询详情
     *
     * @param id 主键
     * @return 查询结果
     */
    @Override
    public VehicleBrandVO load(Integer id) {
        VehicleBrandDO entity = getById(id);

        return VehicleBrandConverter.entityToVo(entity);
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean create(VehicleBrandDTO dto) {
        VehicleBrandDO entity = BeanUtil.toBean(dto, VehicleBrandDO.class);
        return save(entity);
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean edit(VehicleBrandDTO dto) {
        VehicleBrandDO entity = BeanUtil.toBean(dto, VehicleBrandDO.class);
        return updateById(entity);
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return
     */
    @Override
    public Boolean delete(Integer id) {
        return removeById(id);
    }

    /**
     * 获取最大排序
     *
     * @return 查询结果
     */
    @Override
    public Integer getMaxSort() {
        return vehicleBrandMapper.getMaxSort();
    }
}

